<template>
  <el-dialog v-model="finshVisible" :close-on-click-modal="false" @close="handleclose" width="80%">
    <el-form>
      <el-form-item label="">
        <el-tabs v-model="activeName">
          <!-- <el-tab-pane label="智慧主机" name="first"></el-tab-pane> -->
          <el-tab-pane label="智能断路器" name="0"></el-tab-pane>
        </el-tabs>
        <el-table :data="tableData1" row-key="id" ref="refTable" style="width: 100%;">
          <el-table-column type="expand">
            <template #default="props">
              <el-form label-position="left" inline class="demo-table-expand"
                v-if="props.row.dttDevice && props.row.dttDevice.devType">
                <el-form-item label="分路功率：">
                  <span>{{ props.row.syncPower }}W；</span>
                </el-form-item>
                <el-form-item label="分路电流：">
                  <span>{{ props.row.syncIrms }}A；</span>
                </el-form-item>
                <el-form-item label="温度：" v-if="props.row.dttDevice.temp != undefined">
                  <span class="pointerc" @click="
                    handletemperature(
                      'temp',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      '温度'
                    )
                    ">{{ (props.row.dttDevice.temp * 0.1).toFixed(1) }}/℃；</span>
                </el-form-item>
                <el-form-item label="总有功电量：" v-if="props.row.dttDevice.epTotal != undefined">
                  <span class="pointerc" @click="
                    handletemperature(
                      'epTotal',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      '总有功电量'
                    )
                    ">{{
    (props.row.dttDevice.epTotal * 0.1).toFixed(1)
  }}/KW.H；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.eqTotal != undefined" label="总无功电量：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'eqTotal',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      '总无功电量'
                    )
                    ">{{
    (props.row.dttDevice.eqTotal * 0.1).toFixed(1)
  }}/Kvar.H；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.eresidual != undefined" label="剩余用电量：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'eresidual',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      '剩余用电量'
                    )
                    ">{{
    (props.row.dttDevice.eresidual * 0.1).toFixed(1)
  }}/kW.h；</span>
                </el-form-item>

                <el-form-item v-if="props.row.dttDevice.airms != undefined" label="A相相电流：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'airms',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'A相相电流'
                    )
                    ">{{ props.row.dttDevice.airms }}/0.01A；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.birms != undefined" label="B相相电流：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'birms',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'B相相电流'
                    )
                    ">{{ props.row.dttDevice.birms }}/0.01A；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.cirms != undefined" label="C相相电流：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'cirms',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'C相相电流'
                    )
                    ">{{ props.row.dttDevice.cirms }}/0.01A；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.aurms != undefined" label="A相相电压：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'aurms',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'A相相电压'
                    )
                    ">{{ (props.row.dttDevice.aurms * 0.1).toFixed(1) }}/V；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.burms != undefined" label="B相相电压：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'burms',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'B相相电压'
                    )
                    ">{{ (props.row.dttDevice.burms * 0.1).toFixed(1) }}/V；</span>
                </el-form-item>
                <!-- 
                  props.row.dttDevice.devType == 18 ||
                  props.row.dttDevice.devType == 47 ||
                  props.row.dttDevice.devType == 19 ||
                  props.row.dttDevice.devType == 51 -->
                <el-form-item v-if="props.row.dttDevice.curms != undefined
                  " label="C相相电压：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'curms',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'C相相电压'
                    )
                    ">{{ (props.row.dttDevice.curms * 0.1).toFixed(1) }}/V；</span>
                </el-form-item>
                <!-- <el-form-item v-if="props.row.dttDevice.abUrms" label="AB相相电压：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'abUrms',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'AB相相电压'
                    )
                  ">{{ (props.row.dttDevice.abUrms * 0.1).toFixed(1) }}/V</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.bcUrms" label="BC相相电压：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'bcUrms',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'BC相相电压'
                    )
                  ">{{ (props.row.dttDevice.bcUrms * 0.1).toFixed(1) }}/V</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.caUrms" label="CA相相电压：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'caUrms',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'CA相相电压'
                    )
                  ">{{ (props.row.dttDevice.caUrms * 0.1).toFixed(1) }}/V</span>
                </el-form-item> -->
                <el-form-item v-if="props.row.dttDevice.apowerP != undefined" label="A相有功功率：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'apowerP',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'A相有功功率'
                    )
                    ">{{ props.row.dttDevice.apowerP }}/W；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.bpowerP != undefined" label="B相有功功率：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'bpowerP',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'B相有功功率'
                    )
                    ">{{ props.row.dttDevice.bpowerP }}/W；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.cpowerP != undefined" label="C相有功功率：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'cpowerP',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'C相有功功率'
                    )
                    ">{{ props.row.dttDevice.cpowerP }}/W；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.apowerQ != undefined" label="A相无功功率：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'apowerQ',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'A相无功功率'
                    )
                    ">{{ props.row.dttDevice.apowerQ }}/var；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.bpowerQ != undefined" label="B相无功功率：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'bpowerQ',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'B相无功功率'
                    )
                    ">{{ props.row.dttDevice.bpowerQ }}/var；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.cpowerQ != undefined" label="C相无功功率：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'cpowerQ',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'C相无功功率'
                    )
                    ">{{ props.row.dttDevice.cpowerQ }}/W；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.apowerA != undefined" label="A相视在功率：">
                  <span class="pointerc" @click="
                    handletemperature(
                      'apowerA',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'A相视在功率'
                    )
                    ">{{ props.row.dttDevice.apowerA }}/W；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.bpowerA != undefined" label="B相视在功率：">
                  <span style="color: red" class="pointerc" @click="
                    handletemperature(
                      'bpowerA',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'B相视在功率'
                    )
                    ">{{ props.row.dttDevice.bpowerA }}/W；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.cpowerA != undefined" label="C相视在功率：">
                  <span style="color: red" class="pointerc" @click="
                    handletemperature(
                      'cpowerA',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      'C相视在功率'
                    )
                    ">{{ props.row.dttDevice.cpowerA }}/W；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.abcPowerP != undefined" label="合相有功功率：">
                  <span style="color: red" class="pointerc" @click="
                    handletemperature(
                      'abcPowerP',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      '合相有功功率'
                    )
                    ">{{ props.row.dttDevice.abcPowerP }}；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.abcPowerQ != undefined" label="合相无功功率：">
                  <span style="color: red" class="pointerc" @click="
                    handletemperature(
                      'abcPowerQ',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      '合相无功功率'
                    )
                    ">{{ props.row.dttDevice.abcPowerQ }}；</span>
                </el-form-item>
                <el-form-item v-if="props.row.dttDevice.abcPowerA != undefined" label="合相视在功率：">
                  <span style="color: red" class="pointerc" @click="
                    handletemperature(
                      'abcPowerA',
                      props.row.shunt,
                      props.row.imei,
                      props.row.devType,
                      '合相视在功率'
                    )
                    ">{{ props.row.dttDevice.abcPowerA }}；</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="设备号" prop="imei"> </el-table-column>
          <el-table-column label="分路号" prop="shunt"> </el-table-column>
          <el-table-column label="设备名称" prop="remark">
          </el-table-column>
          <el-table-column label="设备属性" prop="description">
          </el-table-column>
          <el-table-column label="状态" prop="isOnline">
            <template #default="scope">
              {{ scope.row.isOnline == 0 ? "离线" : "在线" }}
            </template>
          </el-table-column>
          <el-table-column label="合闸/分闸" prop="devState">
            <template #default="scope">
              <el-switch v-model="scope.row.devState" active-color="red" inactive-color="rgb(147 146 146)" active-text="分"
                inactive-text="合" :active-value="1" :inactive-value="0"
                :before-change="() => handleIsSwitch(scope.row, scope.$index)">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="解锁/锁定" prop="deviceName">
            <template #default="scope">
              <el-switch v-model="scope.row.is_lockState" active-color="red" inactive-color="rgb(147 146 146)"
                active-text="锁定" inactive-text="解锁" :active-value="1" :inactive-value="0"
                :before-change="() => handleIslock(scope.row, scope.$index)">
              </el-switch>
              <div style="color: red;font-size: 10px;" v-if="scope.row.lockState == 1">(本地锁定)</div>
            </template>
          </el-table-column>
          <el-table-column label="使用电量" prop="totalElectricity">
          </el-table-column>
          <el-table-column label="送电时间" prop="totalElectricity">
            <template #default="scope">
              <span v-if="scope.row.emergencyTime">{{ scope.row.emergencyTime }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter, useRoute } from "vue-router";
import api from "../api/api";
export default {
  name: "basetable",
  setup(props, context) {

    const router = useRouter();
    const route = useRoute();
    const activeName = ref("0")
    const tableData1 = context.attrs.tableData1;
    console.log(tableData1)
    // // 在线监测
    if (tableData1.length > 0) {
      for (var i = 0; i < tableData1.length; i++) {
        //是否解锁 0-正常 1-本地锁定(设备端可设置) 2-远程锁定 3-本地锁定和远程锁定
        if (tableData1[i].lockState == 0 || tableData1[i].lockState == 1) {
          tableData1[i].is_lockState = 0;
        } else {
          tableData1[i].is_lockState = 1;
        }
      }
    }
    const finshVisible = ref(context.attrs.finshVisible);
    const switchListId = ref([]);
    const regionIdx = ref("");
    const handleIsSwitch = (row, index) => {
      if (!row.id) {
        return;
      }
      if (row.id) {
        // event==0为合闸  1则是开闸
        switchListId.value.push(row.id);
        regionIdx.value = row.regionId;
        let url = "";
        if (row.devState == 1) {
          url = "backup/switchInfo/switchOpen";
        } else {
          url = "backup/switchInfo/switchOff";
        }
        return new Promise((resolve) => {
          getSwitchopn_off(url);
          return resolve(true)
        })

      }
    };
    const getSwitchopn_off = (url) => {
      api
        .postFormAPI(url, switchListId.value, "POST")
        .then((resdata) => {
          if (resdata.data.code == 1) {
            // if (event == 0) {
            //   ElMessage.success("应急送电成功！");
            // } else {
            //   ElMessage.success("应急断电成功！");
            // }
            // if (resdata.data.data > 0) {
            //   ElMessage.success("操作成功" + resdata.data.data + "台设备");
            // } else {
            //   ElMessage.success("操作" + resdata.data.data);
            // }
            listByRegionId(regionIdx.value, regionIdIndex);
            finshVisible.value = false;
          } else {
            ElMessage.error(resdata.data.message);
          }
        })
        .catch((err) => { });
      switchListId.value = [];
    };
    // 解锁，锁定
    const handleIslock = (row, index) => {
      // event是正常   1-本地锁定(设备端可设置) 2-远程锁定 3-本地锁定和远程锁定
      console.log(index)
      if (!row.id) {
        return;
      }
      return new Promise((resolve) => {
        if (row.id) {
          let message = "";
          let url = "";
          if (row.is_lockState != 0) {
            url = "backup/switchInfo/switchUnlock"; //锁定
            message = "解锁成功！";
          } else {
            url = "backup/switchInfo/switchLock"; //解锁
            message = "锁定成功！";
          }
          api
            .postFormAPI(url, [row.id])
            .then((res) => {
              if (res.data.code == 1) {
                ElMessage.success(message);
                // finshVisible.value = false;
              } else {
                ElMessage.error(res.data.message);
              }
            })
            .catch((err) => { });
        }
        return resolve(true)
      })
    };
    const handletemperature = () => { };
    const handleclose = () => {
      finshVisible.value = false;
      context.emit("handleclose", finshVisible.value);
    };

    return {
      regionIdx,
      switchListId,
      tableData1,
      finshVisible, activeName,
      handleIsSwitch,
      handleIslock,
      handleclose,
      handletemperature,
    };
  },
};
</script>
